package top.igotcha.mapreduce.wordcount;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @time 2021/1/10
 * @auth Gotcha
 * @describe
 * Mapper的泛型
 *  KEYIN:k1的类型     行偏移量        LongWritable
 *  VALUEIN:v1的类型   一行的文本数据   Text
 *  KEYOUT:k2的类型    每个单词        Text
 *  VALUEOUT:v2的类型  固定值，1       LongWritable
 */

public class WordCountMapper extends Mapper<LongWritable,Text, Text,LongWritable> {

    /**
     * @describe 是将k1,v1转换为k2,v2
     * @param key 表示k1
     * @param value 表示v1
     * @param context 表示MapReduce上下文对象
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //第二步要使用的对象
        Text text = new Text();
        LongWritable longWritable = new LongWritable(1);
        //1.对每行数据进行字符串拆分
        String line = value.toString();
        String[] split = line.split(",");
        //2.遍历数组获取每个单词
        for (String word : split) {
            text.set(word);
            //3.将k2,v2写入上下文
            context.write(text,longWritable);
        }
    }
}
